package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.service.ParameterMultiService;
import com.zany.system.vo.ParameterMultiVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.BeansUtil.RequestBeanUtil;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JSONArray;
import com.zany.util.json.JSONException;
import com.zany.util.json.JSONObject;
import com.zany.util.json.JsonTools;

/**
 * 多值参数管理
 * @author chengY
 * @version 1.0
 */
public class ParameterMultiAction extends AbstractActionSupport{

	private static final long serialVersionUID = 7586645319949786829L;
	private static Log logger = LogFactory.getLog(ParameterMultiAction.class);
	private ParameterMultiService parameterMultiService;
	private List<ParameterMultiVO> paramFristGroupList = new ArrayList<ParameterMultiVO>();
	private List<ParameterMultiVO> paramSecondGroupList = new ArrayList<ParameterMultiVO>();
	private List<ParameterMultiVO> paramValueList = new ArrayList<ParameterMultiVO>();
	private ParameterMultiVO parameterMultiVO = new ParameterMultiVO();
	
	public void getFristParam() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("ParameterMultiAction.getFristParam()");}
		response = ServletActionContext.getResponse();
		paramFristGroupList = parameterMultiService.getParamFristGroupList();
		JSONArray listJson = new JSONArray();
		try{
			for(ParameterMultiVO vo:paramFristGroupList){
				JSONObject json = new JSONObject();
				json.put("key",vo.getParamType());
				json.put("value",vo.getParamGrp());
				listJson.put(json);
			}
		}catch(JSONException e){
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		String json = "{ZANY:"+listJson.toString()+"}";
		AjaxTools.exAjax(json,response);
	}
	
	public void getSecondParam() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("ParameterMultiAction.getSecondParam()");}
		response = ServletActionContext.getResponse();
		parameterMultiVO.setParamType(request.getParameter("paramType"));
		paramSecondGroupList = parameterMultiService.getParamSecondGroupList(parameterMultiVO);
		JSONArray listJson = new JSONArray();
		try{
			for(ParameterMultiVO vo:paramSecondGroupList){
				JSONObject json = new JSONObject();
				json.put("key",vo.getParamCode());
				json.put("value",vo.getParamName());
				listJson.put(json);
			}
		}catch(JSONException e){
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		String json = "{ZANY:"+listJson.toString()+"}";
		AjaxTools.exAjax(json,response);
	}
	
	public void getList() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("ParameterMultiAction.getParamList()");}
		response = ServletActionContext.getResponse();
		parameterMultiVO.setParamCode(request.getParameter("paramCode"));
		paramValueList = parameterMultiService.getParamValueList(parameterMultiVO);
		AjaxTools.exAjax("{ZANY:" + JsonTools.lstBeanToJson(paramValueList) + "}", response);
	}
	
	public void insert() throws Exception{
		if(logger.isInfoEnabled()){logger.info("ParameterMultiAction.insert()");}
		response = ServletActionContext.getResponse();
		parameterMultiVO = (ParameterMultiVO)RequestBeanUtil.getReqToBean(ParameterMultiVO.class,request);
		parameterMultiVO.setSeqMult(parameterMultiService.getSeqMult(parameterMultiVO));
		paramValueList = parameterMultiService.getParamValueList(parameterMultiVO);
		for(ParameterMultiVO VO:paramValueList){
			//判断 参数值和参数名不能重复
			if(parameterMultiVO.getOptionText().equals(VO.getOptionText())){
				AjaxTools.exAjax("{\"success\":false,msg:\"此参数名已处在！\"}",response);
				return;
			}else if(parameterMultiVO.getOptionValue().equals(VO.getOptionValue())){
				AjaxTools.exAjax("{\"success\":false,msg:\"此参数值已处在！\"}",response);
				return;
			}
		}
		try{
			Integer seq = parameterMultiService.getSeq(parameterMultiVO)+1;
			parameterMultiVO.setSeq(seq.toString());
			parameterMultiService.insertParamVal(parameterMultiVO);
			AjaxTools.exAjax("{success:true}",response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub	
	}
	public ParameterMultiService getParameterMultiService() {
		return parameterMultiService;
	}
	public void setParameterMultiService(ParameterMultiService parameterMultiService) {
		this.parameterMultiService = parameterMultiService;
	}

}
